Jupyter at Bryn Mawr College |
|||
Public notebooks: /services/public/dblank |
from conx import Network
import numpy as np
import theano.tensor as T
import theano
import random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
%matplotlib inline
net = Network(2, 2, 1, activation_function=T.nnet.sigmoid)
#net.batch = True
# input low and high values:
ilo = -1
ihi = 1
# output low and high values:
olo = 0
ohi = 1
net.set_inputs([[random.random() * 2 - 1, random.random() * 2 - 1] for i in range(1000)])
f = lambda ins: [((ins[0] + ins[1])/2 + 1)/2]
net.set_target_function(f)
net.train(report_rate=20, tolerance=.05)
net.test(10)
def therm(v, size=10):
"""
v - move range [ilo, ihi]
returns thermometer encoding:
[olo, olo, olo]
[ohi, olo, olo]
[ohi, ohi, olo]
[ohi, ohi, ohi]
"""
ro = (ohi - olo) # output range
ri = (ihi - ilo)
t = (v - ilo) / ri # scale from input
t_int = int(t * size) # int part
t_float = round((t * size) - t_int, 2) # left over
# left justified
therm = ([ohi] * t_int + [t_float * ro + olo] + [olo] * size)[:size]
return therm
def untherm(vec):
"""
returns vec decoded - [ilo, ihi]
"""
size = len(vec)
ro = (ohi - olo) # range
total = 0
for v in vec:
total += ((v - olo) / ro)
scale = total/size
return scale * ro + olo
therm(-1)
therm(1)
net = Network(2, 2, 10, activation_function=T.nnet.sigmoid)
net.set_inputs([[random.random() * 2 - 1, random.random() * 2 - 1] for i in range(1000)])
net.set_target_function(lambda ins: therm((ins[0] + ins[1])/2))
def test_net(net, res=20):
m = np.zeros((res, res))
step = (ihi - ilo)/res
for i in range(res):
for j in range(res):
m[i][j] = untherm(net.propagate([ilo + i * step, ilo + j * step]))
plt.matshow(m,
origin="lower", # lower puts 0,0 bottom left
extent=[ilo,ihi,ilo,ihi], # tick label ranges
cmap=cm.coolwarm,
interpolation="none")
plt.xlabel("input(0)")
plt.ylabel("input(1)")
plt.colorbar()
def plot_net(net, res=20, angle=45):
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.arange(ilo, ihi, (ihi - ilo)/res)
Y = np.arange(ilo, ihi, (ihi - ilo)/res)
Z = np.array([[untherm(net.propagate([x, y])) for x in X] for y in Y])
X, Y = np.meshgrid(X, Y)
# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
# Customize the z axis.
ax.set_zlim(olo - 0.01, ohi + 0.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax.view_init(30, angle) # tilt, rotate
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
#display(fig)
#fig.clear()
net.train(report_rate=20, tolerance=0.3)
net.test(10)
test_net(net)
plot_net(net, 45)
from ipywidgets import interact
import ipywidgets as widgets
interact(lambda i1, i2: untherm(net.propagate([i1, i2])),
i1=widgets.FloatSlider(description="i1",min=-1, max=1,step=.1),
i2=widgets.FloatSlider(description="i2",min=-1, max=1,step=.1))
def plot_net_angle(angle):
plot_net(net, angle=angle)
interact(plot_net_angle, angle=widgets.IntSlider(min=-180,max=180,step=10,value=45));
net = Network(2, 2, 1, 10, activation_function=T.nnet.sigmoid)
net.set_inputs([[random.random() * 2 - 1, random.random() * 2 - 1] for i in range(1000)])
net.set_target_function(lambda ins: therm((ins[0] + ins[1])/2))
net.train(report_rate=20, tolerance=0.3)